package com.sscw.task.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.sscw.task.entity.MRecordBattleRecreationPO;
import com.sscw.task.mapper.MRecordBattleRecreationMapper;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;

@Repository
public class MRecordBattleRecreationDAO extends BaseDAO<MRecordBattleRecreationMapper, MRecordBattleRecreationPO> {


    public MRecordBattleRecreationPO findOneByUserId(Long battleId, Long userId) {
        LambdaQueryWrapper<MRecordBattleRecreationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattleRecreationPO::getBattleId, battleId);
        queryWrapper.eq(MRecordBattleRecreationPO::getUserId, userId);
        return getBaseMapper().selectOne(queryWrapper);
    }

    public MRecordBattleRecreationPO findLatestBattleByUserId(Long userId) {
        LambdaQueryWrapper<MRecordBattleRecreationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattleRecreationPO::getBattleDate, LocalDate.now());
        queryWrapper.eq(MRecordBattleRecreationPO::getUserId, userId);
        queryWrapper.orderByDesc(MRecordBattleRecreationPO::getId);
        queryWrapper.last("limit 0,1");
        return getBaseMapper().selectOne(queryWrapper);
    }

    public long countUserTodayBattle(Long userId) {
        LambdaQueryWrapper<MRecordBattleRecreationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattleRecreationPO::getBattleDate, LocalDate.now());
        queryWrapper.eq(MRecordBattleRecreationPO::getUserId, userId);
        return getBaseMapper().selectCount(queryWrapper);
    }
}
